var PALETTE_SYSTEM = "palette_system";
var PALETTE_LINK = "palette_link";
var PALETTE_OTHER = "palette_other";
var PALETTE_MISC = "palette_misc";
var PALETTE_TOOL = "palette_tool";

var paletteViewer = {
	selectionItem : '',
	folders : []

};

function initPaletteViewer() {
	paletteViewer.Event.init();
}

function bulidPalette() {
	// create palette
	// var paletteWindow = $C("div");

	var base_tool = $F('base_tool');
	var dsc_tool = $F('dsc_tool');

	if (base_tool && dsc_tool) {
		var palette = $C("div");
		// init palette
		palette.setAttribute("class", "easyui-accordion");
		palette.setAttribute("data-options", "fit:true,border:false");
		// build
		buildToolFolder(base_tool);
		buildSystemFolder(palette);
		buildLinkFolder(palette);
		buildOtherFolder(palette);
		buildMiscFolder(palette);
		buildUserFolder(palette);
		dsc_tool.appendChild(palette);
	}

}

function buildToolFolder(palette) {
	var folder = createFolder("Tool", "");
	var items = createItems(PALETTE_TOOL);
	palette.appendChild(folder);
	folder.appendChild(items);
	items.setAttribute("data-options", "url:'js/data/tool.json'");
	paletteViewer.folders.push(PALETTE_TOOL);
}

function buildSystemFolder(palette) {
	var folder = createFolder("System", "");
	var items = createItems(PALETTE_SYSTEM);
	palette.appendChild(folder);
	folder.appendChild(items);
	items.setAttribute("data-options", "url:'js/data/system.json'");
	paletteViewer.folders.push(PALETTE_SYSTEM);
}

function buildLinkFolder(palette) {
	var folder = createFolder("Links", "");
	var items = createItems(PALETTE_LINK);
	palette.appendChild(folder);
	folder.appendChild(items);
	items.setAttribute("data-options", "url:'js/data/link.json'");
	paletteViewer.folders.push(PALETTE_LINK);
}

function buildOtherFolder(palette) {
	var folder = createFolder("Other", "");
	var items = createItems(PALETTE_OTHER);
	palette.appendChild(folder);
	folder.appendChild(items);
	items.setAttribute("data-options", "url:'js/data/other.json'");
	paletteViewer.folders.push(PALETTE_OTHER);
}

function buildMiscFolder(palette) {
	var folder = createFolder("Misc", "");
	var items = createItems(PALETTE_MISC);
	palette.appendChild(folder);
	folder.appendChild(items);
	items.setAttribute("data-options", "url:'js/data/misc.json'");
	paletteViewer.folders.push(PALETTE_MISC);
}

function buildUserFolder(palette) {

}

function createFolder(title, icon) {
	var folder = $C("div");
	folder.setAttribute("title", title);
	folder.setAttribute("data-options", "iconCls:'" + icon + "'");
	folder.setAttribute("style", "overflow:auto;padding:10px;");
	return folder;
}

function createItems(id) {
	var items = $C("ui");
	items.setAttribute("class", "easyui-tree");
	items.setAttribute("id", id);
	return items;
}

function updatePalette() {

}

function restoreOtherFolder(folderId) {
	for (var i in paletteViewer.folders) {
		if (i === undefined) {
			continue;
		}
		var id = paletteViewer.folders[i];
		if (id != folderId) {
			$('#' + id).tree('reload');
		}

	}
	var cmd = _dss_editor._command._commandsManager.undostack[_dss_editor._command._commandsManager.undostack.length - 1];
	if ( cmd instanceof CreateLinkCommand && !cmd.isComplete) {
		cmdstack.pop();
	}
}

function restoreAllFolder(canvas) {
	var node = $('#' + PALETTE_TOOL).tree('find', 1);
	if (node) {
		$('#' + PALETTE_TOOL).tree('select', node.target);
		paletteViewer.selectionItem = node.text;
		restoreOtherFolder(PALETTE_TOOL);
		canvas.entity.style.cursor = 'default';
		canvas.update();
	}
}